package cn.blog.controller.front;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.addBean.RzglArtInfo;
import cn.blog.entity.origin.ClassBean;
import cn.blog.service.front.ChaxunService;
import cn.blog.service.front.CompileHeadService;
import cn.blog.service.front.RizhiglService;
@WebServlet("/front/chaxun")
public class ChaxunServlet extends BaseServlet {
	boolean isStr2Num(String str) {   
		try {  
			Integer.parseInt(str);  
		    return true;  
		} catch (NumberFormatException e) {  
		    return false;  
		} 
	}  
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CompileHeadService chs = new CompileHeadService();
		// 获取用户id
		String userId = req.getSession().getAttribute("userId").toString();
		int uid = Integer.parseInt(userId);
		ChaxunService cs = new ChaxunService();
		String aid= req.getParameter("artId");
		
		if(aid!=""&&aid!=null) {
			cs.updateState(Integer.parseInt(aid));
		}
		
		String artId = req.getParameter("text");// 博客账号
		int article = 0;
		if(isStr2Num(artId)) {
			article = Integer.parseInt(artId);
		}
		String clas = req.getParameter("text");// 文章类型名称
		RizhiglService rs = new RizhiglService();
		List<RzglArtInfo> art = rs.getArticle(uid, "已发表", article, clas);
		if (art.size() == 0) {
			art = rs.getArticleAll(uid,"已发表");// 调用查询所有博客文章方法
		}
		PrintWriter out = resp.getWriter();
		
		String artJson = "";
		for(int i=0;i<art.size();i++) {
			/*String list = art.get(i).getArtId()+","+art.get(i).getTitle()+","+art.get(i).getArtClass()+","
		+art.get(i).getArtTime()+",";*/
			artJson +=  art.get(i).getArtId()+","+ art.get(i).getTitle()+
					","+ art.get(i).getArtClass()+","+ art.get(i).getArtTime()+";";
			/*artJson = artJson+list;*/
		}
		//截取字符串，去掉最后的逗号
		
			artJson = artJson.substring(0, artJson.length() - 1);
		
		/*//去掉城市json的最后一个字符
		artJson = artJson.substring(0, artJson.length());*/
		//将json格式的字符串输出
		resp.setCharacterEncoding("utf-8");
		out.print(artJson);
	}
}
